// Highcharts plugin for displaying value information in the legend
// Author: Torstein Hønsi
// License: MIT license
// Last revision: 2013-07-29
// to format the last price, we added it the pilot level redraw when new points arrive
(function (H) {
    H.Series.prototype.point = {}; // The active point

    Highcharts.Legend.prototype.regenerateLabels = function() {
        var legendOptions = this.options;
        if ( !isValued( this.options ) ) return;
        H.each(this.allItems, function (item) {
        item.legendItem.attr({
          text: legendOptions.labelFormat ?
            H.format(legendOptions.labelFormat, item) :
            legendOptions.labelFormatter.call(item)
          //css: H.merge( item.visible ? this.itemStyle : this.itemHiddenStyle )
        });
      });
    }

Highcharts.Legend.prototype.setDirty = function( dirty ) {
  this.chart.isDirtyLegend = dirty;
  this.chart.isDirtyBox = dirty;
}

Highcharts.Legend.prototype.resetTitle = function() {
    this.options.title.text = "";
    if( this.chart.tooltip ) {
      //this.options.title.text = chart.tooltip.tooltipFooterHeaderFormatter( usedPoint, false);
      this.title.attr( { text : this.options.title.text });
    }
}
Highcharts.Legend.prototype.getHoverPoints = function () {
  var chart = this.chart;
  var hoverPoints = chart.hoverPoints;

  if (!hoverPoints && chart.hoverPoint) {
    hoverPoints = [chart.hoverPoint];
  }
  return hoverPoints;
}

Highcharts.Legend.prototype.regenerateTitle = function() {
  var chart = this.chart;
  if (!chart) return;
  var hoverPoints = this.getHoverPoints();
  if (hoverPoints == null) {
    if (chart.series.length > 0 && chart.series[0].xData.length > 0) {
      var x = null;
      chart.series.forEach(function(s) {
        //go get me a last price as a point
        var index = s.xData.length - 1;
        if (index > 0 && (!isValued(x) || x < s.xData[index])) {
          x = s.xData[index];
        }
      });
      this.options.title.text = Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', x);
    } else {
      //no data..nothing to do here
      return;
    }
  } else {
    var usedPoint = {
      x: hoverPoints[0].x,
      key: hoverPoints[0].x,
      series: hoverPoints[0].series,
      y: hoverPoints[0].y,
    };
    this.options.title.text = chart.tooltip.tooltipFooterHeaderFormatter(usedPoint, false);
  }
  if (chart.tooltip) {
    //this.options.title.text = chart.tooltip.tooltipFooterHeaderFormatter( usedPoint, false);
    this.title.attr({ text: this.options.title.text });
  }
}

   Highcharts.Legend.prototype.resetHoverPoints = function () {
    var chart = this.chart;
    if (isValued(chart)) {
        var hoverPoints = this.getHoverPoints();
        if (hoverPoints) {
            hoverPoints.forEach(function(p) {
                p.setState();
            });
        }
        chart.hoverPoints = null;
        chart.hoverPoint = null;
    }
  }

    Highcharts.Legend.prototype.reformat = function (current) {
      var chart = this.chart;
      if ( !chart ) return;
      if ( !chart.options.legend || chart.options.legend.enabled == false ) return;

      if (getBoolean(current)) {
        chart.legend.resetHoverPoints();
        H.each(chart.series, function (s) {
                s.point = null;
        });

      } else {
          var hoverPoints = this.getHoverPoints();
          if (hoverPoints) {
            H.each(hoverPoints, function (point) {
            point.series.point = point;
            });
          }
      }
      this.regenerateLabels();
      this.regenerateTitle();
      this.setDirty( true );
      this.render();
    }

    H.Chart.prototype.callbacks.push(function (chart) {
        $(chart.container).bind('mousemove', function () {
            chart.legend.reformat();

        });
    });
    // Hide the tooltip but allow the crosshair
    H.Tooltip.prototype.defaultFormatter = function () { return false; };


}(Highcharts));